package tk.cful.servlets;

import tk.cful.core.DBManager;
import tk.cful.core.FileEntity;
import tk.cful.core.UserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

/**
 * User: Root
 * Date: 09.11.13
 * Time: 16:33
 */
@WebServlet(name = "WorkspaceServlet", urlPatterns = {"/workspace"})
public class WorkspaceServlet extends HttpServlet {

    private Logger log = Logger.getLogger(WorkspaceServlet.class.getName());
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession(true);
        String userName = (String) session.getAttribute("user_name");
        if (userName == null || userName.equals(""))
            response.sendRedirect("/cful/index.html");
        else {
            PrintWriter pw = response.getWriter();
            UserEntity user = DBManager.getInstance().findUserByName(userName);
            List<FileEntity> files = DBManager.getInstance().findFilesByUserID(user.getUserID());

            pw.println("<html>\n" +
                    "<head>\n" +
                    "    <title>Workspace</title><script type=\"text/javascript\" src=\"/cful/jquery-1.10.2.min.js\"></script>" +
                    "<script language=\"JavaScript\" src=\"crypto/core.js\" type=\"text/javascript\"></script>\n" +
                    "<script language=\"JavaScript\" src=\"crypto/enc-base64.js\" type=\"text/javascript\"></script>\n" +
                    "<script language=\"JavaScript\" src=\"crypto/md5.js\" type=\"text/javascript\"></script>\n" +
                    "<script language=\"JavaScript\" src=\"crypto/evpkdf.js\" type=\"text/javascript\"></script>\n" +
                    "<script language=\"JavaScript\" src=\"crypto/cipher-core.js\" type=\"text/javascript\"></script>\n" +
                    "<script language=\"JavaScript\" src=\"crypto/rabbit.js\" type=\"text/javascript\"></script>" +
                    "<script src=\"/cful/fileSaver.js\"></script>\n" +
                    "<script src=\"/cful/dl.js\"></script></head>\n" +
                    "\n" +
                    "<body leftmargin=\"0\" topmargin=\"0\" bgcolor=\"#FFFFFF\" marginwidth=\"0\" marginheight=\"0\">\n" +
                    "<br>\n" +
                    "\n" +
                    "<form method=\"post\" name=\"form1\" action=\"/cful/upload.html\">\n" +
                    "    <table cellspacing=\"0\" cellpadding=\"1\" width=\"98%\" align=\"center\" bgcolor=\"#CCCCCC\" border=\"0\">\n" +
                    "        <tbody>\n" +
                    "        <tr valign=\"top\">\n" +
                    "            <td>\n" +
                    "                <table cellspacing=\"0\" cellpadding=\"4\" width=\"100%\" bgcolor=\"#EEEEEE\" border=\"0\">\n" +
                    "                    <tbody>\n" +
                    "                    <tr bgcolor=\"#000000\">\n" +
                    "                        <td bgcolor=\"#999999\" valign=\"top\" width=\"20%\">\n" +
                    "                            <span style=\"color: #FFFFFF; \"><b>Storage</b></span>\n" +
                    "                            <a href=\"/cful/security_check?op=logout\">Log out</a>\n" +
                    "                        </td>\n" +
                    "                    </tr>\n" +
                    "\n" +
                    "                    <tr>\n" +
                    "                        <td bgcolor=\"#DDDDDD\" align=\"left\">\n" +
                    "                            <p><input type=\"submit\" value=\"Upload file\" name=\"Submit\"></p>\n" +
                    "                        </td>\n" +
                    "                    </tr>\n" +
                    "\n" +
                    "                    <tr valign=\"top\">\n" +
                    "                        <td align=\"center\">\n" +
                    "                            <table border=\"1\" bordercolor=\"#000000\" style=\"background-color:#eeeeee\" width=\"100%\"\n" +
                    "                                   cellpadding=\"2\" cellspacing=\"\">\n" +
                    "                                <thead>\n" +
                    "                                <tr>\n" +
                    "                                    <th style='width: 50%;'>File name</th>\n" +
                    "                                    <th style='width: 50%;'>Download URL</th>\n" +
                    "                                </tr>\n" +
                    "                                </thead>");

            for (FileEntity file : files) {
                pw.println("<tr><td>");
                pw.println(file.getFileName());
                pw.print("</td><td><input class=\"pass\"> <a href=\"javascript: void(0);\" onclick=\"");
                pw.print("window.download('/cful/upload_file?fileName=" + file.getHashName().replace("\\", "/"));
                pw.print("', this);\">");
                log.info(file.getHashName());
                pw.println("Download");
                pw.println("</a> <a href=\"download.jsp?name=" + file.getHashName().replace("\\", "/")+"\">");
                log.info(file.getHashName());
                pw.println("Shared link");
                pw.println("</a> <img style='display:none;' src='loading.gif' class='loading'></td></tr>");
            }

            pw.println("</table>\n" +
                    "\n" +
                    "                        </td>\n" +
                    "                    </tr>\n" +
                    "                    </tbody>\n" +
                    "                </table>\n" +
                    "            </td>\n" +
                    "        </tr>\n" +
                    "        </tbody>\n" +
                    "    </table>\n" +
                    "</form>\n" +
                    "</body>\n" +
                    "</html>");
        }
    }
}
